package org.tgcloud.group.core;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.tgcloud.group.web.exception.ToolBoxException;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/28 0028.
 * 对象常用方法工具类
 */
public class BeanUtil {

    /**
     * 此类不需要实例化
     */
    private BeanUtil() {
    }

    /**
     * 判断对象是否为null
     *
     * @param object 需要判断的对象
     * @return 是否为null
     */
    public static boolean isNull(Object object) {
        return null == object;
    }

    /**
     * 判断对象是否不为null
     *
     * @param object 需要判断的对象
     * @return 是否不为null
     */
    public static boolean nonNull(Object object) {
        return null != object;
    }

    /**
     * 判断对象是否为空，如果为空，直接抛出异常
     *
     * @param object       需要检查的对象
     * @param errorMessage 异常信息
     * @return 非空的对象
     */
    public static Object requireNonNull(Object object, String errorMessage) {
        if (null == object) {
            throw new NullPointerException(errorMessage);
        }
        return object;
    }

    public static Map getValue(Object object)
    {
        Map map = Maps.newHashMap();
        Class c = object.getClass();
        Method[] methods = c.getMethods();
        for (int i = 0; i < methods.length; i++)
        {
            String methodName = methods[i].getName();
            if (methodName.startsWith("get"))
            {
                try {
                    Object value = methods[i].invoke(object);
                    if (value != null)
                    {
                        String key=methodName.substring(3);
                        key=key.substring(0,1).toUpperCase()+key.substring(1);
                        map.put(key, value);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    System.out.println("error:" + methods[i].getName());
                }
            }
        }
        return map;
    }

    /**
     * 判断列表是否为空
     * @param list
     * @return
     */
    public static boolean isNullList(List list) {
        return list == null || list.size() == 0;
    }

    /**
     * 检测多个参数是否为空
     * @param list
     * @return
     */
    public static boolean isNullOrEmpty(List<String> list)
    {
        if (list == null || list.size() == 0)
        {
            return true;
        }
        for (String s : list)
        {
            if (!Strings.isNullOrEmpty(s))
            {
                return false;
            }
        }

        return true;
    }

    public static boolean isNullMap(Map map)
    {
        return isNull(map) || map.keySet().size() == 0;
    }

    /**
     * 对象转Map
     * @param bean bean对象
     * @return Map
     */
    @SuppressWarnings("unchecked")
    public static <T> Map<String, Object> beanToMap(T bean, List<String> args) {

        if (bean == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(bean.getClass());
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                /*过滤class属性*/
                if (false == key.equals("class")) {
                    Method getter = null;
                    if (args == null || BeanUtil.isNullList(args))
                    {
                        getter = property.getReadMethod();
                    }
                    else
                    {
                        if (args.contains(key))
                        {
                            getter = property.getReadMethod();
                        }
                    }
                    if (getter != null)
                    {
                        Object value = getter.invoke(bean);
                        if (null != value) {
                            map.put(key, value);
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new ToolBoxException(e);
        }
        return map;
    }

    /**
     * 获得Bean类属性描述
     * @param clazz Bean类
     * @return PropertyDescriptor数组
     * @throws IntrospectionException
     */
    public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws IntrospectionException {
        return Introspector.getBeanInfo(clazz).getPropertyDescriptors();
    }

    /**
     * 数组是否为空
     * @param array 数组
     * @return 是否为空
     */
    public static <T> boolean isEmptyArray(T[] array) {
        return array == null || array.length == 0;
    }

    /**
     * 数组中是否包含元素
     * @param array 数组
     * @param value 被检查的元素
     * @return 是否包含
     */
    public static <T> boolean contains(T[] array, T value) {
        final Class<?> componetType = array.getClass().getComponentType();
        boolean isPrimitive = false;
        if(null != componetType) {
            isPrimitive = componetType.isPrimitive();
        }
        for (T t : array) {
            if(t == value) {
                return true;
            }else if(false == isPrimitive && null != value && value.equals(t)) {
                return true;
            }
        }
        return false;
    }
}
